<?php

include_once "function_xml_basic.php";

$xpath_root = "/MINI/NETWORK/PPPOES";
$xpath_node = "/INTERFACE";

function GetMaxDevNumberForPPPOE()
{
	$ret = 2;
	return $ret;
}

function GetAttrListForPPPOE()
{
	$returns = array("Name", "Type", "Enabled", "ETH", "USER", "PASSWORD", "DEMAND", "DNSTYPE", "DNS1", "DNS2", "DEFAULTROUTE", "PIDFILE");
	return $returns;
}

function GetNodeNameFromXpath($xpath_node)
{
	$node_name = substr($xpath_node,1);
	return $node_name;
}

function GetXmlPPPOEDevList()
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node;
	$key_array = GetAttrListForPPPOE();
	
	$list = GetAttributList($query_string, $key_array);
	#for ($i = 0; $i < count($list); $i ++)
	#{
	#	foreach ( $key_array as $key)
	#		print $i. $key. " = ". $list[$i][$key]. "\n";
	#}
	return $list;
}

function GetXmlPPPOEDevCount()
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node;
	$ret = GetNumberOfNode($query_string);
	return $ret;
}
# $ret = GetXmlPPPOEDevCount();
# print $ret;

function GetXmlPPPOEDevByName($name)
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node;

        $ret_list = GetAllNodeValue($query_string. "[@Name='". $name. "']");
	$result = $ret_list -> item(0);
	
	$key_array = GetAttrListForPPPOE();
	foreach ( $key_array as $key)
	{
		$ret_node[$key] = $result->getAttribute($key);
		#print $key. " -> ". $ret_node[$key]. "\n";
	}
	return $ret_node;
}
#$node = GetXmlPPPOEDevByName("pppoe0");

function GetXmlPPPOEByNumber($n)
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node;
        $ret_list = GetAllNodeValue($query_string. "[". $n."]");
	$result = $ret_list -> item(0);
	
	$key_array = GetAttrListForPPPOE();
	foreach ( $key_array as $key){
		$ret_node[$key] = $result->getAttribute($key);
		#print $key. " -> ". $ret_node[$key]. "\n";
	}
	
	return $ret_node;
}
#GetXmlPPPOEByNumber(0);

function GetNetDevPPPOEInfoByEth($eth)
{
	$dev_list =  GetXmlPPPOEDevList();

	foreach ($dev_list as $p){
		if ($p["ETH"] == $eth)
			return $p;
	}
	return FALSE;
}

function DelXmlSpecialOnePPPOEByName($name)
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node;
	DelNodeByAttr($query_string, "Name", $name);
}
#DelXmlSpecialOnePPPOEByName("pppoe1");

function AppendXmlPPPOEDev($in_array)
{
	global $xpath_root;
	global $xpath_node;
	
	$node_name = GetNodeNameFromXpath($xpath_node);
	$query_string = $xpath_root;
	AppendAllAttrOfNode($query_string, $node_name, $in_array);
}
#$list["Name"] = "pppoe2";
#$list["Type"] = "PPPOE";
#$list["ETH"] = "eth0";
#AppendXmlPPPOEDev($list);

function InsertXmlPPPOEDev($number, $in_array)
{
	global $xpath_root;
	global $xpath_node;
	
	$node_name = GetNodeNameFromXpath($xpath_node);
	$query_string_from = $xpath_root. $xpath_node. '['. $number. ']';
	InsertOneNode($query_string_from, $node_name, $in_array);
}
#$list["USER"] = "USERNAME";
#$list["PASS"] = "PASSWORD";
#$list["ETH"] = "eth3";
#InsertXmlPPPOEDev(1,$list);

function SetXmlPPPOEDevByNumber($number, $in_array )
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node. "[". $number. "]";
	EditAllAttrOfNode($query_string, $in_array);
}

function SetXmlPPPOEDevByName($name, $in_array )
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node. "[@Name='". $name. "']";
	EditAllAttrOfNode($query_string, $in_array);
}
function DelXmlAllPPPOEDev()
{
	global $xpath_root;
	global $xpath_node;
	$query_string = $xpath_root. $xpath_node;

	DelSpecialNode($query_string);
}

function ApplyXmlPPPOEDev()
{
	$command = "";
	$config_path_root = "/etc/ppp/pppoe.conf";
	$pid_path_root = "/var/run/pid.pppoe";
	$i = 0;
	$p_list = GetXmlPPPOEDevList();
	foreach ($p_list as $p){
		$config_path = $config_path_root. $i;
		$pid_path = $pid_path_root. $i;
		$user = $p['USER'];
		$pass = $p['PASS'];
		$eth = $p['ETH'];
		$command = 'sed -e "s&^USER=.*&USER=\''. $user. '\'& < '. $config_path_root. ' > '. $config_path;
		#print $command. "\n";
		$ret = shell_exec($command);
		$command = 'sed -e "s&^ETH=.*&ETH=\''. $eth. '\'& < '. $config_path_root. ' > '. $config_path;
		#print $command. "\n";
		$ret = shell_exec($command);
		$ready_command = '
			cp -rf /etc/ppp/pap-secrets /etc/ppp/pap-secrets-bak; \
    			cp -rf /etc/ppp/chap-secrets /etc/ppp/chap-secrets-bak ';
		$ret = shell_exec($ready_command);
	
		$command = 'grep -v "^'. $user. '|^\"'. $user. '\"" /etc/ppp/pap-secrets-bak >/etc/ppp/pap-secrets';
		#print $command. "\n";
		$ret = shell_exec($command);
		$command = 'echo "\"'. $user. '\"	*	\"'. $pass. '\"" >> /etc/ppp/pap-secrets';
		#print $command. "\n";
		$ret = shell_exec($command);
		
		$command = 'grep -v "^'. $user. '|^\"'. $user. '\"" /etc/ppp/chp-secrets-bak >/etc/ppp/chap-secrets';
		#print $command. "\n";
		$ret = shell_exec($command);
		$command = 'echo "\"'. $user. '\"	*	\"'. $pass. '\"" >> /etc/ppp/chap-secrets';
		#print $command. "\n";
		$ret = shell_exec($command);
	}
	return $ret;
}

?>
